/*
 * dioPacketHandler.h
 *
 * Created: 6/13/2013 3:01:50 AM
 *  Author: Tim
 */ 


#ifndef DIOPACKETHANDLER_H_
#define DIOPACKETHANDLER_H_

#include <stdbool.h>
#include "../../packets/status.h"
#include "../../features/dio/dio.h"
#include "../../packets/structs/msgHeaderDefs.h"
#include "../../packets/structs/dioPacketStructs.h"
#include "../../packets/CRC.h"
#include "../../features/tmr/tmr.h"
#include "../../communication/comPort.h"

static inline status_t dioInitPollingHandler(dioInitPolling_t *packet)
{
		
	tmrAlloc_t intTmr;
	uint16_t frequency;
	
	if(daqAlloc.standAlone.daqStandAlone) return OPP_FAILED;//this function is not standalone type
	
	frequency = (packet->PARAM1.DPFRQH<<8) | packet->PARAM2.DPFRQL;
	
	intTmr = tmrCheckFrequency(frequency);
	if(intTmr == TIMER_NULL)
	{
		intTmr = tmrCheckAvailability(frequency);
		if(intTmr == TIMER_NULL) return OPP_FAILED;
		else
		{
			tmrSetupFreq(intTmr,frequency);
		}
	}
	daqAlloc.dio_int = intTmr;
	return OPP_SUCCESSFUL;
	
}
static inline void dioCurrentStateHandler()
{
	dioCurrentState_t packet;
	
	packet.Header		= packetHeader;
	packet.msgHeader	= dioCurrentState_mh;
	packet.PARAM1.DPMH	= dioReadHigh();
	packet.PARAM2.DPML	= dioReadLow();
	packet.PARAM1.RESERVED = 0;
	
	crcAppend(&packet,packetSizeLookup(dioCurrentState_mh));
	comPortPut(&packet,packetSizeLookup(dioCurrentState_mh));
}

static inline status_t dioOutSetHandler(dioOutSet_t *packet)
{
	
	if(daqAlloc.standAlone.daqStandAlone) return OPP_FAILED;//this function is not standalone type
	
	if(packet->PARAM1.dio12)	dioSet(12);
	if(packet->PARAM1.dio11)	dioSet(11);
	if(packet->PARAM1.dio10)	dioSet(10);
	if(packet->PARAM1.dio9)		dioSet(9);
	if(packet->PARAM1.dio8)		dioSet(8);
	
	if(packet->PARAM2.dio7)		dioSet(7);
	if(packet->PARAM2.dio6)		dioSet(6);
	if(packet->PARAM2.dio5)		dioSet(5);
	if(packet->PARAM2.dio4)		dioSet(4);
	if(packet->PARAM2.dio3)		dioSet(3);
	if(packet->PARAM2.dio2)		dioSet(2);
	if(packet->PARAM2.dio1)		dioSet(1);
	if(packet->PARAM2.dio0)		dioSet(0);
}
static inline status_t dioOutClrHandler(dioOutClr_t *packet)
{
	
	if(daqAlloc.standAlone.daqStandAlone) return OPP_FAILED;//this function is not standalone type
	
	if(packet->PARAM1.dio12)	dioClr(12);
	if(packet->PARAM1.dio11)	dioClr(11);
	if(packet->PARAM1.dio10)	dioClr(10);
	if(packet->PARAM1.dio9)		dioClr(9);
	if(packet->PARAM1.dio8)		dioClr(8);
	
	if(packet->PARAM2.dio7)		dioClr(7);
	if(packet->PARAM2.dio6)		dioClr(6);
	if(packet->PARAM2.dio5)		dioClr(5);
	if(packet->PARAM2.dio4)		dioClr(4);
	if(packet->PARAM2.dio3)		dioClr(3);
	if(packet->PARAM2.dio2)		dioClr(2);
	if(packet->PARAM2.dio1)		dioClr(1);
	if(packet->PARAM2.dio0)		dioClr(0);
}
static inline status_t dioDirSetHandler(dioDirSet_t *packet)
{
	
	if(daqAlloc.standAlone.daqStandAlone) return OPP_FAILED;//this function is not standalone type
	
	if(packet->PARAM1.dio12)	dioDirSet(12);
	if(packet->PARAM1.dio11)	dioDirSet(11);
	if(packet->PARAM1.dio10)	dioDirSet(10);
	if(packet->PARAM1.dio9)		dioDirSet(9);
	if(packet->PARAM1.dio8)		dioDirSet(8);
	
	if(packet->PARAM2.dio7)		dioDirSet(7);
	if(packet->PARAM2.dio6)		dioDirSet(6);
	if(packet->PARAM2.dio5)		dioDirSet(5);
	if(packet->PARAM2.dio4)		dioDirSet(4);
	if(packet->PARAM2.dio3)		dioDirSet(3);
	if(packet->PARAM2.dio2)		dioDirSet(2);
	if(packet->PARAM2.dio1)		dioDirSet(1);
	if(packet->PARAM2.dio0)		dioDirSet(0);
}
static inline status_t dioDirClrHandler(dioDirClr_t *packet)
{
	
	if(daqAlloc.standAlone.daqStandAlone) return OPP_FAILED;//this function is not standalone type
	
	if(packet->PARAM1.dio12)	dioDirClr(12);
	if(packet->PARAM1.dio11)	dioDirClr(11);
	if(packet->PARAM1.dio10)	dioDirClr(10);
	if(packet->PARAM1.dio9)		dioDirClr(9);
	if(packet->PARAM1.dio8)		dioDirClr(8);
	
	if(packet->PARAM2.dio7)		dioDirClr(7);
	if(packet->PARAM2.dio6)		dioDirClr(6);
	if(packet->PARAM2.dio5)		dioDirClr(5);
	if(packet->PARAM2.dio4)		dioDirClr(4);
	if(packet->PARAM2.dio3)		dioDirClr(3);
	if(packet->PARAM2.dio2)		dioDirClr(2);
	if(packet->PARAM2.dio1)		dioDirClr(1);
	if(packet->PARAM2.dio0)		dioDirClr(0);
}

#endif /* DIOPACKETHANDLER_H_ */